import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/wcto.vue'
import user from '../components/user.vue'
import roles from '../components/roles.vue'
import rights from '../components/rights.vue'
import goods from '../components/goods.vue'
import params from '../components/params.vue'
import categories from '../components/categories'
import orders from '../components/orders'
import reports from '../components/reports'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: welcome },
        { path: '/users', component: user },
        { path: '/roles', component: roles },
        { path: '/rights', component: rights },
        { path: '/goods', component: goods },
        { path: '/params', component: params },
        { path: '/categories', component: categories },
        { path: '/orders', component: orders },
        { path: '/reports', component: reports }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const tokenvalue = sessionStorage.getItem('token')
    if (!tokenvalue) {
      next('/login')
    } else {
      next()
    }
  }
})
export default router
